/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.controller;

/**
 *
 * @author Bayu
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.bayu.services.dao.AverageMouldingDao;
import com.bayu.services.entity.AverageMoulding;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/tpsaverage-update.htm")
public class  TPSAverageUpdate {
private AverageMouldingDao dao;

    @Autowired
    public void setDao(AverageMouldingDao dao) {
        this.dao = dao;

    }

    @RequestMapping(method=RequestMethod.GET)
    public void displayForm(ModelMap map, String id) {
        List list = this.dao.getById(Long.parseLong(id));
        map.addAttribute("data", list);
    }

    @RequestMapping(method=RequestMethod.POST)
    public String processForm(@ModelAttribute("AverageMoulding") AverageMoulding entity,
            BindingResult result, ModelMap map, String date, HttpServletRequest request) {

        String id = request.getParameter("id");
        String type = request.getParameter("type");
        String strReturn ="";

         DateFormat formarter = new SimpleDateFormat("yy-MM-dd");
            Date tgl;

            //exchangeCurrency.setDate(tgl);
            if(result.hasFieldErrors("date")){
                try {
                    tgl = formarter.parse(date);
                    entity.setDate(tgl);
                } catch (ParseException ex) {
                    Logger.getLogger(GalvaUpdate.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        if (id != null) {
            entity.setId(Long.parseLong(id));
            this.dao.update(entity);
                if(type.equals("linemoulding")){
                    strReturn= "redirect:view-average-moulding-data.htm?msg=Data has been updated";
                 }
                 if(type.equals("tapping")){
                    strReturn= "redirect:view-average-tapping-data.htm?msg=Data has been updated";
                 }

                if(type.equals("cnc")){
                    strReturn= "redirect:view-average-cnc-data.htm?msg=Data has been updated";
                 }
                 if(type.equals("mccomp")){
                    strReturn= "redirect:view-average-machining-comp-data.htm?msg=Data has been updated";
                 }

                if(type.equals("mcp1")){
                    strReturn= "redirect:view-average-machining-p1-data.htm?msg=Data has been updated";
                 }
            
                 if(type.equals("operation-mc")){
                    strReturn= "redirect:view-average-operation-machining-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mouldfoundry")){
                    strReturn= "redirect:view-average-mould-foundry-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mouldpms")){
                    strReturn= "redirect:view-average-mould-pms-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mouldsm60")){
                    strReturn= "redirect:view-average-mould-sm60-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("moulddisa")){
                    strReturn= "redirect:view-average-mould-disa-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mould2ml")){
                    strReturn= "redirect:view-average-mould-2ml-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("shaft2w")){
                    strReturn= "redirect:view-average-mould-shaft2w-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("shaft4w")){
                    strReturn= "redirect:view-average-mould-shaft4w-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("machining1")){
                    strReturn= "redirect:view-tps-average-machining1-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("machining2")){
                    strReturn= "redirect:view-tps-average-machining2-data.htm?msg=Data has been updated";
                 }
            
                 return strReturn;
        } else {
            return "redirect:tpsaverage-update.htm";
        }

    }
}
